import { defineConfig, UserConfigExport } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import vitePluginMd from "vite-plugin-md";
import { VitePWA } from "vite-plugin-pwa";
import copy from 'rollup-plugin-copy' //引入插件

import {
  vitePluginMdConfig,
  vueConfig,
  vitePwaConfig,
  viteCompressions,
} from "./config/plugins";
import { useGlobalVars } from "./src/utils/variables";
import viteCompression from "vite-plugin-compression";
import { visualizer } from "rollup-plugin-visualizer";

const { BaseRouter: base } = useGlobalVars();
// 配置信息
const config: UserConfigExport = defineConfig({
  base,
  plugins: [
    vue(vueConfig),
    vitePluginMd(vitePluginMdConfig),
    VitePWA(vitePwaConfig),
    viteCompression(viteCompressions),
    visualizer(),
    copy({
      targets: [
        { src: 'assets/street_files/**/*', dest: 'dist/public/street_files' }
      ]
    })
  ],
  server: {
    https: false, // 是否开启 https
    open: true, // 是否自动在浏览器打开
    port: 3020, // 端口号
    host: "0.0.0.0",
    cors: true,
    proxy: {
      "/engine": {
        target: "http://piecloud.piesat.cn/engine/factory",
        changeOrigin: true,

        rewrite: (path) => path.replace(/^\/engine/, ""),
      },
      "/api": {
        target: "https://engine.piesat.cn/hr_data/api",
        changeOrigin: true,

        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
  resolve: {
    // 路径别名
    alias: {
      "@": resolve(__dirname, "./src"),
      "~": resolve(__dirname, "./packages"),
    },
  },
  css: {
    // 配置预编译器
    preprocessorOptions: {
      less: {
        additionalData: `@import "@/styles/index.less";`,
        charset: false
      },
    },
  },
  build: {
    target: 'modules',
    outDir: 'dist', //指定输出路径
    assetsDir: 'assets', // 指定生成静态资源的存放路径
    ssrManifest: false,
    manifest: false,
    sourcemap: false,
    minify: "terser",
    terserOptions: {
      keep_classnames: true,
    },
    // 多页面
    rollupOptions: {
      input: {
        main: resolve(__dirname, "./index.html"),
        // manage: resolve(__dirname, "./src/applications/dataManage/views/index.html"),
        // monitor: resolve(__dirname, "./src/applications/dataMonitor/views/index.vue"),
        // server: resolve(__dirname, "./src/applications/dataServer/views/index.vue"),
      }
    }

  },
});

export default config;
